﻿using System;
using System.IO;
using System.Linq;
using KeyCore.Domain.Model;
using KeyCore.Framework.Contexts;
using KeyCore.Framework.DbInit;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace KeyCore.Framework.Tests
{ 
    [DeploymentItem(@"EntityFramework.SqlServer.dll")]
    [DeploymentItem(@"EntityFramework.dll")]
    [TestClass]
    public class Context
    {   
       
        public Context()
        {
            AppDomain.CurrentDomain.SetData("DataDirectory",@"c:\Users\wroc2_001");
                                                            //Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ""));
            CanRegenerateDb(); /* generate db if not exist with seed method in DbInit 
                                * (db name and sql instance defined in config file as KeyCore.Test / KeyCoreDbTest.mdf
                                */
        }

        
        [TestMethod]
        public void CanRegenerateDb()
        {
            try
            {
                using (var db = new KeyCoreDbContext())
                {
                    db.Database.Initialize(true);
                }
            }
            catch (Exception ex)
            {
                
                throw ex;
            }

        }

        // after CanRegenerateDb();
        [TestMethod]
        public void DatabaseExist()
        {
            using (var dc = new KeyCoreDbContext())
            {
                Assert.IsTrue(dc.Database.Exists());
            }
        }

        [TestMethod]
        public void CanCreateUser()
        {
            using (var dc = new UserContext())
            {
                var ur = new UserRoles();
                dc.Users.Add(new User() {UserName = "Test",
                    Email = "email@email.pl", 
                    Password ="password",
                    CreatedDate = DateTime.Now,
                    PasswordSalt = "salt",
                    IsApproved = true,
                    UserRole =dc.UserRoles.Single(r=>r.RoleName == "administrator")
          });
                dc.SaveChanges();
                Assert.AreEqual("Test",dc.Users.Single(u=>u.UserName == "Test").UserName);
                Assert.AreEqual(2, dc.Users.Count());
            }
            
        }
    }
}
